package com.scsj.supermarket.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.scsj.supermarket.R;
import com.scsj.supermarket.event.FirstEvent;
import com.scsj.supermarket.utils.RxBus;

import java.util.ArrayList;
import java.util.List;

/**
 *  搜索结果页面选择全部按钮
 */

public class SelectAllMarkAdapter extends RecyclerView.Adapter<SelectAllMarkAdapter.ViewHolderA> {
    private Context mContext;
    private List<String> mList;
    private String selectCondition;
    private List<Boolean> isClicks;//控件是否被点击,默认为false，如果被点击，改变值，控件根据值改变自身颜色
    public static int currentPosition;
    public boolean isClearAllMark= false;

    public SelectAllMarkAdapter(Context context, List<String> list,int allposion) {
        mContext = context;
        mList = list;
        isClicks = new ArrayList<>();
        for(int i = 0;i<mList.size();i++){
            isClicks.add(false);
        }
        if(allposion != -1) {
            isClicks.set(allposion, true);
        }
        currentPosition = allposion;
    }
    @Override
    public SelectAllMarkAdapter.ViewHolderA onCreateViewHolder(ViewGroup parent, int viewType) {
        //此处动态加载ViewHolder的布局文件并返回holder
        View view = LayoutInflater.from(mContext).inflate(R.layout.all_sort_item_layout, parent, false);
        SelectAllMarkAdapter.ViewHolderA holderA = new SelectAllMarkAdapter.ViewHolderA(view);
        return holderA;
    }
    @SuppressLint("ResourceAsColor")
    @Override
    public void onBindViewHolder(final SelectAllMarkAdapter.ViewHolderA holder, final int position) {
        //此处设置Item中view的数据
        holder.mTextView.setText(mList.get(position));
        if (position== mList.size()-1){
            holder.lineView.setVisibility(View.GONE);
        }else {
            holder.lineView.setVisibility(View.VISIBLE);
        }

        if (isClearAllMark){
            clearAllSelectedState();
        }

        if(isClicks.get(position)){
            holder.mTextView.setTextColor(Color.parseColor("#FF314A"));
        }else {
            holder.mTextView.setTextColor(Color.parseColor("#575656"));
        }


        holder.mTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                clearAllSelectedState();
                if(currentPosition != position) {
                    isClicks.set(position, true);
//                    selectMark = mList.get(position);
                    isClearAllMark = false;
                    currentPosition = position;
                }else {
                    currentPosition = -1;
//                    selectMark = null;
                }

                isClicks.set(position,true);
                isClearAllMark = false;
                selectCondition= mList.get(position);
                RxBus.getDefault().post(new FirstEvent("selectItem",selectCondition));
                notifyDataSetChanged();
            }
        });
    }
    @Override
    public int getItemCount() {
        //生成的item的数量
        return mList.size();
    }
    //Item的ViewHolder以及item内部布局控件的id绑定
    class ViewHolderA extends RecyclerView.ViewHolder{
        TextView mTextView;
        View lineView;
        public ViewHolderA(View itemView) {
            super(itemView);
            mTextView =  (TextView) itemView.findViewById(R.id.mark_name);
            lineView =  itemView.findViewById(R.id.single_line_view);
        }
    }

    //清空选中的所有按钮
    private void clearAllSelectedState(){
        for (int i = 0; i < isClicks.size(); i++) {
            isClicks.set(i, false);
        }
    }
}
